using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKASCHP
    {
        public DataRow Convert2KASCHP(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKASCHP(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KAPMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KAPSCHEM"] = " ".PadRight(5 ); 
         dwReturn["KAPWAKZ"] = " ".PadRight(1 ); 
         dwReturn["KAPRANG"] = " ".PadRight(4 ); 
         dwReturn["KAPSA"] = " ".PadRight(1 ); 
         dwReturn["KAPKOMM"] = " ".PadRight(6 ); 
         dwReturn["KAPKOMMP"] = " ".PadRight(2 ); 
         dwReturn["KAPLSTKZ"] = " ".PadRight(1 ); 
         dwReturn["KAPFELD"] = " ".PadRight(8 ); 
         dwReturn["KAPSTAMM"] = " ".PadRight(1 ); 
         dwReturn["KAPMERK"] = " ".PadRight(8 ); 
         dwReturn["KAPVART"] = " ".PadRight(5 ); 
         dwReturn["KAPSUMKZ"] = " ".PadRight(1 ); 
         dwReturn["KAPSKOM"] = " ".PadRight(6 ); 
         dwReturn["KAPSPOS"] = " ".PadRight(2 ); 
         dwReturn["KAPSZUS"] = " ".PadRight(2 ); 
         dwReturn["KAPMATKZ"] = " ".PadRight(1 ); 
         dwReturn["KAPCHAR1"] = " ".PadRight(1 ); 
         dwReturn["KAPCHAR2"] = " ".PadRight(1 ); 
         dwReturn["KAPCHAR3"] = " ".PadRight(1 ); 
         dwReturn["KAPCHAR4"] = " ".PadRight(1 ); 
         dwReturn["KAPCHAR5"] = " ".PadRight(1 ); 
         dwReturn["KAPSDB"] = " ".PadRight(1 ); 
         dwReturn["KAPMENKZ"] = " ".PadRight(1 ); 
         dwReturn["KAPEB"] = " ".PadRight(1 ); 
         dwReturn["KAPBEM"] = " ".PadRight(20 ); 
         dwReturn["KAPFILLER"] = " ".PadRight(14 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKASCHP(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKASCHP(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKASCHP(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKASCHP(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKASCHP(dwLocal, ITXDBVERSION);
            }
            UpdateKASCHP(dwLocal);
        }
        public void InsertKASCHP(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KASCHP(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKASCHP(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KASCHP WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KASCHP  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KASCHP";
            Return = Result;
            return Return;
        }
        public void DeleteKASCHP(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KASCHP   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKASCHP(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KASCHP SET "
                    +"KAPMANDANT= :KAPMANDANT,"
                    +"KAPSCHEM= :KAPSCHEM,"
                    +"KAPWAKZ= :KAPWAKZ,"
                    +"KAPRANG= :KAPRANG,"
                    +"KAPSA= :KAPSA,"
                    +"KAPKOMM= :KAPKOMM,"
                    +"KAPKOMMP= :KAPKOMMP,"
                    +"KAPLSTKZ= :KAPLSTKZ,"
                    +"KAPFELD= :KAPFELD,"
                    +"KAPSTAMM= :KAPSTAMM,"
                    +"KAPMERK= :KAPMERK,"
                    +"KAPVART= :KAPVART,"
                    +"KAPSUMKZ= :KAPSUMKZ,"
                    +"KAPSKOM= :KAPSKOM,"
                    +"KAPSPOS= :KAPSPOS,"
                    +"KAPSZUS= :KAPSZUS,"
                    +"KAPMATKZ= :KAPMATKZ,"
                    +"KAPCHAR1= :KAPCHAR1,"
                    +"KAPCHAR2= :KAPCHAR2,"
                    +"KAPCHAR3= :KAPCHAR3,"
                    +"KAPCHAR4= :KAPCHAR4,"
                    +"KAPCHAR5= :KAPCHAR5,"
                    +"KAPSDB= :KAPSDB,"
                    +"KAPMENKZ= :KAPMENKZ,"
                    +"KAPEB= :KAPEB,"
                    +"KAPBEM= :KAPBEM,"
                    +"KAPFILLER= :KAPFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KAPMANDANT", OracleType.NVarChar).Value = dwData["KAPMANDANT"].ToString(); 
          or1.Parameters.Add("KAPSCHEM", OracleType.NVarChar).Value = dwData["KAPSCHEM"].ToString(); 
          or1.Parameters.Add("KAPWAKZ", OracleType.NVarChar).Value = dwData["KAPWAKZ"].ToString(); 
          or1.Parameters.Add("KAPRANG", OracleType.NVarChar).Value = dwData["KAPRANG"].ToString(); 
          or1.Parameters.Add("KAPSA", OracleType.NVarChar).Value = dwData["KAPSA"].ToString(); 
          or1.Parameters.Add("KAPKOMM", OracleType.NVarChar).Value = dwData["KAPKOMM"].ToString(); 
          or1.Parameters.Add("KAPKOMMP", OracleType.NVarChar).Value = dwData["KAPKOMMP"].ToString(); 
          or1.Parameters.Add("KAPLSTKZ", OracleType.NVarChar).Value = dwData["KAPLSTKZ"].ToString(); 
          or1.Parameters.Add("KAPFELD", OracleType.NVarChar).Value = dwData["KAPFELD"].ToString(); 
          or1.Parameters.Add("KAPSTAMM", OracleType.NVarChar).Value = dwData["KAPSTAMM"].ToString(); 
          or1.Parameters.Add("KAPMERK", OracleType.NVarChar).Value = dwData["KAPMERK"].ToString(); 
          or1.Parameters.Add("KAPVART", OracleType.NVarChar).Value = dwData["KAPVART"].ToString(); 
          or1.Parameters.Add("KAPSUMKZ", OracleType.NVarChar).Value = dwData["KAPSUMKZ"].ToString(); 
          or1.Parameters.Add("KAPSKOM", OracleType.NVarChar).Value = dwData["KAPSKOM"].ToString(); 
          or1.Parameters.Add("KAPSPOS", OracleType.NVarChar).Value = dwData["KAPSPOS"].ToString(); 
          or1.Parameters.Add("KAPSZUS", OracleType.NVarChar).Value = dwData["KAPSZUS"].ToString(); 
          or1.Parameters.Add("KAPMATKZ", OracleType.NVarChar).Value = dwData["KAPMATKZ"].ToString(); 
          or1.Parameters.Add("KAPCHAR1", OracleType.NVarChar).Value = dwData["KAPCHAR1"].ToString(); 
          or1.Parameters.Add("KAPCHAR2", OracleType.NVarChar).Value = dwData["KAPCHAR2"].ToString(); 
          or1.Parameters.Add("KAPCHAR3", OracleType.NVarChar).Value = dwData["KAPCHAR3"].ToString(); 
          or1.Parameters.Add("KAPCHAR4", OracleType.NVarChar).Value = dwData["KAPCHAR4"].ToString(); 
          or1.Parameters.Add("KAPCHAR5", OracleType.NVarChar).Value = dwData["KAPCHAR5"].ToString(); 
          or1.Parameters.Add("KAPSDB", OracleType.NVarChar).Value = dwData["KAPSDB"].ToString(); 
          or1.Parameters.Add("KAPMENKZ", OracleType.NVarChar).Value = dwData["KAPMENKZ"].ToString(); 
          or1.Parameters.Add("KAPEB", OracleType.NVarChar).Value = dwData["KAPEB"].ToString(); 
          or1.Parameters.Add("KAPBEM", OracleType.NVarChar).Value = dwData["KAPBEM"].ToString(); 
          or1.Parameters.Add("KAPFILLER", OracleType.NVarChar).Value = dwData["KAPFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
